package com.china08.yxyapi.api.manage.edu.platform.statistics.resourcepandect;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceSubNameCountService;
import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 资源统计    各科目资源
 * 
 */
@CrossOrigin
@RestController
public class ResourceSubNameCountController extends YxyApiBaseController{
	@Autowired
	public ResourceSubNameCountService resourceSubNameCountService;
	@Autowired
	public YxyResSubjectRepository yxyResSubjectRepository;
	@ApiOperation(value = "资源统计    各科目资源")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/edu/platform/statistics/resourcepandect/resourcesubname/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response,@RequestBody ReqModel req) {
        RespModel model = new RespModel();
        Integer schoolBureauLevel =getProfile(request, response).getSchoolBureauLevel();
        String districtCode=null;
        if (schoolBureauLevel==0) {
        	districtCode=getProfile(request, response).getSchoolAreaIdDistrict();
		}else if(schoolBureauLevel==1){
			districtCode=getProfile(request, response).getSchoolAreaIdCity();
		}else if(schoolBureauLevel==2){
			districtCode=getProfile(request, response).getSchoolAreaIdProvince();
		}
        Map<String, Object> countLst = null;
        
        DecimalFormat df = new DecimalFormat("######0.00");
        
        //饼形图
        List<Object[]>  countLstObj=new ArrayList<Object[]>();
        //调用此方法查询资源数据总览数据
        if (req.stageName == null || req.stageName.equals("") || req.stageName.equals("undefined")) {			
        	countLst=resourceSubNameCountService.statisticsResourcePhaseCount2("",districtCode, schoolBureauLevel);
        	Integer zong=0;
        	for (String key1 : countLst.keySet()) {
        		zong+=Integer.parseInt(countLst.get(key1).toString());
			}
        	Object[]  objs=null;
        	for (String key : countLst.keySet()) {
        		objs=new Object[2];
        		
        		//将科目的编码改成科目的名称
        		YxyResSubject yxyResSubject=yxyResSubjectRepository.findById(key);
        		if (yxyResSubject!=null) {
        			objs[0]=yxyResSubject.getName();
        			objs[1]=Double.parseDouble(df.format(Double.parseDouble(countLst.get(key).toString())));
        			countLstObj.add(objs);
        		}
        		
        		
			}
		}else{
			countLst=resourceSubNameCountService.statisticsResourcePhaseCount2(req.stageName,districtCode, schoolBureauLevel);
			Integer zong=0;
        	for (String key1 : countLst.keySet()) {
        		zong+=Integer.parseInt(countLst.get(key1).toString());
			}
        	Object[]  objs=null;
        	for (String key : countLst.keySet()) {
        		objs=new Object[2];
        		YxyResSubject yxyResSubject=yxyResSubjectRepository.findById(key);
        		
        		if (yxyResSubject!=null) {
        			objs[0]=yxyResSubject.getName();
        			objs[1]=Double.parseDouble(df.format(Double.parseDouble(countLst.get(key).toString())));
        			countLstObj.add(objs);
				}
        		
        		
			}
		}
        
        String[] subjectNameList=new String[]{"语文","数学","英语","思想政治","物理","化学","历史","生物","地理"};
        List<Object[]> subList=new ArrayList<Object[]>();
               if (countLstObj.size()>0) {
                	Object[] obj=null;
                	for (String subjectName : subjectNameList) {
                		
                		for (Object[] objects : countLstObj) {
            				if (subjectName.equals(objects[0])) {
            					obj=new Object[2];
            					obj[0]=objects[0];
            					obj[1]=objects[1];
            					subList.add(obj);
            				}
            			}
        			}
        			
        		}
               //将新组合的list里的学段名称值连接在一起
               String phaseName="";
               for (Object[] objects : subList) {
            	   phaseName+=objects[0]+",";
       		   }
                if (countLstObj.size()>subList.size()) {
                	for (Object[] countLstObj1 : countLstObj) {
                		    
                		    //搜索字符串中是否包含数组里的元素
            				if (phaseName.indexOf(countLstObj1[0].toString())==-1) {
            					subList.add(countLstObj1);
       						}
        			}
        		}
        model.countLstObj=subList;
        return model;
    }
    protected static class RespModel extends YxyApiRespModel {
		//资源类型饼形图的数据
		public List<Object[]>  countLstObj;
    }
    protected static class ReqModel extends YxyApiReqModel {
        //学段名称
    	public String stageName;
    }
}
